package com.java.interview.thread;

import org.junit.Test;

/**
 * Created by lyc on 2016-03-25.
 */
//线程静态常量困惑?
//1、比那一切的静态优化可以打乱代码的执行顺序
//2.jvm的动态优化也会打乱代码的执行顺序
//3.硬件可以通过乱序执行来优化其性能
public class InterviewStaticPuzzle {
    static  boolean answerReady=false;
    static  int answer=0;
    static Thread t1=new Thread(){
        public void  run(){
            answer=42;
            answerReady=true;
        }
    };
    static  Thread t2=new Thread(){
        public void  run(){
            if (answerReady)
                System.out.println("the meaning of life is："+answer);
            else
                System.out.println("i don't know the answer");
        }
    };

    @Test
    public void testInterviewStaticPuzzle() throws InterruptedException {
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        //输出结果：Process finished with exit code 0
        //注意更糟糕的是 while（！answerReady）这样的条件可能永远都不会变成true
    }
}
